/*
 ### jQuery Multiple File Upload Plugin v1.47 - 2010-03-26 ###
 * Home: http://www.fyneworks.com/jquery/multiple-file-upload/
 * Code: http://code.google.com/p/jquery-multifile-plugin/
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 ###
*/

/*# AVOID COLLISIONS #*/
;if(window.jQuery) (function($){
/*# AVOID COLLISIONS #*/
 
	// plugin initialization
	$.fn.Xselect = function(options){
		var _this = this;
		if(_this.length==0) return _this;
        var defaults = {
			onchanges: null
		}
		var options = $.extend(defaults, options || {});
		var sel_option;
		
		_this.init = function(select){
			
			var div = $(select).parents('.xselect').length==0 ? $(select).wrap('<div class="xselect"></div>') : $(select).parents('.xselect');
			
			
			_this.option($(select));
			_this.selected(select);
			$(select).bind('change',function(){
				//$(select).parents('.xselect').next().find('select').change();
				//alert()
				_this.option($(this));
			});
			
		}
		
		_this.option = function(select){
			var li = [];
			var p = select.parents('.xselect');
			sel_option = select.find('option:selected');
			p.find('ul').remove();
			p.find('h3').remove();
			select.find('option').each(function(){
				var o = $(this);
				li.push ('<li><a href="#">'+o.text()+'</a></li>')
				
			});
			//alert(select.html())
			p.append('<h3><em><i>'+sel_option.text()+'</i><em></h3>');
			p.append('<ul>'+li.join('\n')+'</ul>');
			
			
		}
		
		_this.selected = function(select){
		    var lis = $(select).parents('.xselect').find('ul li');
			lis.click(function(){
				var index = lis.index(this);
				
				$(select).get(0).selectedIndex = index;
				$(select).next('h3').find('i').text($(this).text());
				$(select).change();
				alert(index)
				//_this.option($(select).parents('.xselect').next().find('select'));
				//if (options.onchanges) options.onchanges(select);
			});
			
		}
		
		
		_this.each(function(){
			
			_this.init(this);
		});
		
		return _this;
		
	}
	
	
	

})(jQuery);

